package eu.szemraj.utils.time.inactivity {
   import flash.events.EventDispatcher;   

   import eu.szemraj.utils.time.StopWatch;

   import flash.display.Stage;
   import flash.events.Event;
   import flash.events.TimerEvent;
   import flash.utils.Timer;      

   /**
    *  Quest Agency
    *  www: http://www.questagency.pl
    *  email: info@questagency.pl
    *  @author Rafal Szemraj ( rafal@questagency.pl )
    */
   public class Inactivity extends EventDispatcher {

      private var _stage:Stage;
      private var _intervalValue:int;
      private var _stopWatch:StopWatch;
      private var _checkTimer:Timer;
      private var _activationEventsNames:Array = [];

      public function Inactivity( stage:Stage, miliseconds:int ) {
      	
         _stage = stage;
         _intervalValue = miliseconds;
         _stopWatch = new StopWatch( );
         _checkTimer = new Timer( miliseconds, 1 );
         _checkTimer.addEventListener( TimerEvent.TIMER_COMPLETE, userInactive );
      }

      public function addUserActivationEvent( eventName:String ):Boolean {
      	
         if( _activationEventsNames.lastIndexOf( eventName ) != -1 ) return false;
         _stage.addEventListener( eventName, userInput );
         _activationEventsNames.push( eventName );
         return true;
      }

      public function removeUserActivationEvent( eventName:String ):Boolean {
      	
         if( _activationEventsNames.lastIndexOf( eventName ) != -1 ) {
      		
            _activationEventsNames.splice( _activationEventsNames.lastIndexOf( eventName ), 1 );
            return true;
         }
         return false;
      }

      public function onUserActive( event:Event = null ):void {
      	
         userInput( event );
      }

      public function start():void {
      	
         if( _checkTimer.running ) return;
         _stopWatch.start( );
         _checkTimer.start( );
      }

      public function stop():void {
      	
         _checkTimer.reset( );
         var totalEvents:int = _activationEventsNames.length;
         for( var i:int = 0; i < totalEvents ; ++i ) {
         	
            _stage.removeEventListener( _activationEventsNames[ i ] as String, userInput );
         }
      }

      protected function userInactive( event:Event ):void {
      	
         var inactivityEvent:InactivityEvent = new InactivityEvent( InactivityEvent.INACTIVE );
         inactivityEvent.milliseconds = _checkTimer.delay;
         this.dispatchEvent( inactivityEvent );
			
         _stopWatch.start( );
      }

      protected function userInput(event:Event):void {
      	
         if ( !_checkTimer.running ) {
         	
            var inactivityEvent:InactivityEvent = new InactivityEvent( InactivityEvent.ACTIVATED );
            inactivityEvent.milliseconds = _stopWatch.time + _checkTimer.delay;
				
            dispatchEvent( inactivityEvent );
				
            _stopWatch.stop( );
         }
		
         _checkTimer.reset( );
         _checkTimer.start( );	
      }
   }
}
